<?php
//课程查询 begin
$goods_id = Goods::find()->select('id')->groupBy('name')->orderBy('id asc')->asArray()->column();
$goods=Goods::find()->where(['id' => $goods_id, 'is_booking' => 1]);
$pages_all = new Pagination(['totalCount' => $goods->count(), 'pageSize' => '9']);
$goods_all = $goods->offset($pages_all->offset)->limit($pages_all->limit)->all();
//下面是查询所有的分类
$category = \Yii::$app->cacheManage->updateCategorygoodsformobileCache();
$tree = (new Tree($category))->get_tree_array();
$class='';
//下面是课程筛选
if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'course') {
    \Yii::$app->response->format = Response::FORMAT_JSON;
    //获取课程类型
    $course_category = \Yii::$app->request->post('course_category');
    $category_id = Cate::find()->where(['name' => $course_category])->asArray()->one();
    $category_ids = [];
    if ($category_id['parent_id'] == 0) {
        $category_ids[] = explode(',', $category_id['arrchild_id']);
    } else {
        $category_ids[] = $category_id['id'];
    }
    //获取上课形式
    $class_modality = '';
    switch (\Yii::$app->request->post('class_modality')) {
        case '一对一':
            $class_modality = '0';
            break;
        case '小班制':
            $class_modality = '1';
            break;
        case '普通制':
            $class_modality = '2';
            break;
        default:
            $class_modality = '全部';
    }
    //下面是获取上课时间
    $course_time = \Yii::$app->request->post('course_time');
    $course_noon = \Yii::$app->request->post('course_noon');
    $goods = Goods::find()->where(['is_booking' => 1]);
    if ($course_category == '全部' && $class_modality == '全部' && $course_time == '全部' && $course_noon == '全部') {
        $goods_id = Goods::find()->select('id')->groupBy('name')->orderBy('id asc')->asArray()->column();
        $goods = Goods::find()->where(['id' => $goods_id, 'is_booking' => 1]);
    } else {
        if ($course_category != '全部') {
            if ($category_id['parent_id'] == 0) {
                foreach ($category_ids as $key => $val) {
                    $goods->andWhere(['in', 'cat_id', $val])->groupBy('name');
                }
            } else {
                $goods->andWhere(['in', 'cat_id', $category_ids])->groupBy('name');
            }
        }
        if ($class_modality != '全部') {
            //去goods_attr表里面查出来当前选择的授课方式的所有的id
            if($class_modality=='一对一'){
                $class_modality_num=0;
            }elseif ($class_modality=='小班制'){
                $class_modality_num=1;
            }else{
                $class_modality_num=2;
            }
            $course_type = GoodsAttr::find()->where(['obj_type' => 'teacher_form', 'value' => $class_modality_num])->asArray()->all();
            $ids = ArrayHelper::getColumn($course_type, 'goods_id');
            $goods->andWhere(['in', 'id', $ids])->groupBy('name');
        }
        if ($course_time != '全部') {
            $class_time_id = GoodsClassTime::find()->select('goods_id')->where(['like', 'class_day', $course_time])->column();
            $goods->andWhere(['in', 'id', $class_time_id])->groupBy('name');
        }
        if ($course_noon != '全部') {
            $class_noon_id = GoodsClassTime::find()->select('goods_id')->where(['>=', 'start_time', $course_noon])->column();
            $goods->andWhere(['in', 'id', $class_noon_id])->groupBy('name');
        }
    }
    $pages = new Pagination(['totalCount' => $goods->count(), 'pageSize' => '9']);
    $goods_all=$goods->offset($pages_all->offset)->limit($pages_all->limit)->all();
    $ajax_object = new AjaxObject();
    $strs = '';
    $pages_html = LinkPager::widget(['pagination' => $pages, 'prevPageCssClass' => 'prevclass', 'nextPageCssClass' => 'nextclass']);
    if (!empty($goods_all)) {
        foreach ($goods_all as $k => $v) {
            if (!empty($v->teacher->img)&&isset($v->teacher->img)) {
                $picture = $v->teacher->img;
            } else {
                $picture = '/images/noperson.png';
            }
            if ($v['is_booking'] == 0) {
                $strs .= '<li class="list1">
                                <img src="' . Html::encode(isset($v->cate->pc_image)&&!empty($v->cate->pc_image)?$v->cate->pc_image:'/images/nopic.png') . '" width="100%" height="150px" />
                                <div class="teacher"><img src="' . $picture . '" width="80px" height="81px"/></div>
                                <span id="cj-touch-hidden" >' . Html::encode($v['id']) . '</span>
                                <p>课程名称：' . Html::encode($v['name']) . '</p>
                                <p>课程老师：' . Html::encode(isset($v->teacher) ? $v->teacher->name : '') . '</p>
                                <p>课程学费：' . Html::encode(isset($v->price)?$v->price:'') . '</p>
                                <p>课程课时：' . Html::encode('') . '</p>
                             </li>';
            } else {
                $strs .= '<li class="list1">
                                <img src="' . Html::encode(isset($v->cate->pc_image)&&!empty($v->cate->pc_image)?$v->cate->pc_image:'/images/nopic.png') . '" width="100%" height="150px" />
                                <div class="teacher"><img src="' . $picture . '" width="80px" height="81px"/></div>
                                <span id="cj-touch-hidden" >' . Html::encode($v['id']) . '</span>
                                <p>课程名称：' . Html::encode($v['name']) . '</p>
                                <p>课程老师：' . Html::encode(isset($v->teacher) ? $v->teacher->name : '') . '</p>
                                <p>课程学费：' . Html::encode(isset($v->price)?$v->price:'') . '</p>
                                <p>课程课时：' . Html::encode('') . '</p>
                                <div id="can-appointment"><img src="/images/appoint.png"><p>可预约</p></div>
                             </li>';
            }
        }
    } else {
        $strs = "<div id='sorry'>
                          <img src='/images/no2.png'/>
                       </div>";
    }
    $ajax_object->pages = $pages_html;
    $ajax_object->content = $strs;
    $ajax_object->data = $course_category;
    return $ajax_object;
}
//课程查询 end
//点击课程列表页面进入详情页面的代码
if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'details') {
    \Yii::$app->response->format = Response::FORMAT_JSON;
    if (\Yii::$app->request->post('hidden_id') == null) {
        throw new BadRequestHttpException(\Yii::t('frontend', '非法请求'), 400);
    }
    $details_id = \Yii::$app->request->post('hidden_id');
    $details = Goods::find()->where(['id' => $details_id])->asArray()->one();
    if ($details == null) {
        throw new NotFoundHttpException(\Yii::t('frontend', '信息不存在'));
    }
    //下面是查询老师得相关得信息，通过goods_attr表来查询
    $goods_teacher_id=GoodsAttr::find()->where(['goods_id' => $details['id'],'obj_type' => 'teacher'])->asArray()->one();
    $teacher=Teacher::find()->where(['id' => $goods_teacher_id['attribute_id']])->asArray()->one();
    $mechanic = Mechanic::find()->where(['id' => $details['mechanic_id']])->asArray()->one();
    //下面是查询授课方式
    $goods_attr_teacherform=GoodsAttr::find()->where(['goods_id' => $details['id'],'obj_type' => 'teacher_form'])->asArray()->one();
    switch ($goods_attr_teacherform['attribute_id']) {
        case 0:
            $goods_attr_teacherform['attribute_id'] = '一对一';
            break;
        case 1:
            $goods_attr_teacherform['attribute_id'] = '小班制';
            break;
        case 2:
            $goods_attr_teacherform['attribute_id'] = '普通制';
            break;
    }
    $ajax_object = new AjaxObject();
    $string = '';
    $picture = '';
    $class = '';
    $price_class = '';
    $price_free = '';
    if (!empty($teacher['img']) && isset($teacher['img'])) {
        $picture_url = 'background:url(' . $teacher['img'] . ') center top no-repeat';
    } else {
        $picture_url = 'background:url(/images/images/nopic.png) center top no-repeat';
    }
    $picture .= '<div class="cj-details-subban" style="' . $picture_url . '"></div>';
    //下面是获取所有的上课时间
    $course_time_all=GoodsClassTime::find()->where(['goods_id' => $details_id])->asArray()->all();
    if(!empty($course_time_all)&&isset($course_time_all)){
        foreach ($course_time_all as $key => $val) {
            if ($key == 0) {
                $string .= '<div class="cj-descript">
                            <h2>课程介绍</h2>
                            <div class="cj-descrip-top">
                            <ul>
                                <li><h3>课程名称</h3><p>' . Html::encode($details['name']) . '</p></li>
                                <div class="clear"></div>
                                <li><h3>授课形式</h3><p>' . Html::encode(isset($course['teache_form']) ? $course['teache_form'] : '') . '</p></li>
                                <div class="clear"></div>
                                <li><h3>授课老师</h3><p>' . Html::encode(isset($teacher['name']) ? $teacher['name'] : '') . '</p></li>
                                <div class="clear"></div>
                                <li><h3>授课机构</h3><P>' . Html::encode(isset($mechanic['name']) ? $mechanic['name'] : '') . '</P></li>
                                <div class="clear"></div>
                            </ul>
                             <ul class="cj-descrip-img">
                                <img src="/images/code-details.jpg" width="122px" height="122px">
                                <p>微信扫一扫</p>
                                <p>预约免费学</p>
                             </ul>
                            </div>
                            <div class="clear"></div>
                            <ul class="cj-descrip-teacher">
                                <li><h3>老师简介</h3><p>' . HtmlPurifier::process(isset($teacher['description']) ? $teacher['description'] : '') . '</p></li>
                                <div class="clear"></div>
                            </ul>
                       </div>
                       <div class="cj-time">
                            <h2>上课时间（免费学）</h2>';
            }
            $string .= '';
            if ($key == count($course_time_all) - 1) {
                $string .= '</div>
                       <div class="cj-couinfo">
                            <h2>课程描述</h2><p>' . HtmlPurifier::process(isset($course['description']) ? $course['description'] : '') . '</p>
                       </div>';
            }
        }
    }else{
        $string.='<div class="cj-descript">
                            <h2>课程介绍</h2>
                            <div class="cj-descrip-top">
                            <ul>
                                <li><h3>课程名称</h3><p>' . Html::encode($details['name']) . '</p></li>
                                <div class="clear"></div>
                                <li><h3>授课形式</h3><p>' . Html::encode(isset($course['teache_form']) ? $course['teache_form'] : '') . '</p></li>
                                <div class="clear"></div>
                                <li><h3>授课老师</h3><p>' . Html::encode(isset($teacher['name']) ? $teacher['name'] : '') . '</p></li>
                                <div class="clear"></div>
                                <li><h3>授课机构</h3><P>' . Html::encode(isset($mechanic['name']) ? $mechanic['name'] : '') . '</P></li>
                                <div class="clear"></div>
                            </ul>
                             <ul class="cj-descrip-img">
                                <img src="/images/code-details.jpg" width="122px" height="122px">
                                <p>微信扫一扫</p>
                                <p>预约免费学</p>
                             </ul>
                             </div>
                            <div class="clear"></div>
                            <ul class="cj-descrip-teacher">
                                <li><h3>老师简介</h3><p>' . HtmlPurifier::process(isset($teacher['description']) ? $teacher['description'] : '') . '</p></li>
                                <div class="clear"></div>
                            </ul>
                       </div>
                       <div class="cj-time">
                            <h2>上课时间</h2>
                            <p>暂无可预约课程</p>
                       </div>
                       <div class="cj-couinfo">
                            <h2>课程描述</h2><p>' . HtmlPurifier::process(isset($course['description']) ? $course['description'] : '') . '</p>
                       </div>';
    }
    $ajax_object->data = $picture;
    $ajax_object->content = $string;
    return $ajax_object;
}

//下面是打卡得ajax处理